#include "SdkMic.h"
#include "SdkMediaCommon.h"
#include "InSdkIniConfig.h"

#define DEF_TAG             "Mic"

struct frame g_SdkMic_Frame;
static Com_s32 g_SdkMic_Handle = -1;
static int g_SdkMic_SleepTime = 10;

Com_s32 g_SdkMic_Default_Volume = 8;
Com_s32 g_SdkMic_Default_Gain = 3;

void SdkMic_Set_VolumeOrGain(Com_u8 v_sta,Com_s32 v_val,Com_u8 g_sta,Com_s32 g_val)
{
    if(v_sta == DEF_COM_ENABLE)
    {
        if (ak_ai_set_volume(g_SdkMic_Handle, v_val))
        {
            ak_print_error_ex(MODULE_ID_APP, "ak_ao_set_volume failed\n");
        }
    }

    if(g_sta == DEF_COM_ENABLE)
    {
        if (ak_ai_set_gain(g_SdkMic_Handle, g_val))
        {
            ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_gain failed\n");
            return;
        }
    }
}

void SdkMic_Reset_VolumeAndGain(void)
{
    if (ak_ai_set_volume(g_SdkMic_Handle, g_SdkMic_Default_Volume))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ao_set_volume failed\n");
    }

    if (ak_ai_set_gain(g_SdkMic_Handle, g_SdkMic_Default_Gain))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_gain failed\n");
        return;
    }
}

int Ext_SdkMic_Init(void)
{
    int gain = g_SdkMic_Default_Gain;
    int volume = g_SdkMic_Default_Volume;

    struct ak_audio_in_param param;
    struct ak_audio_nr_attr default_ai_nr_attr = {-40, 0, 1};
    struct ak_audio_agc_attr default_ai_agc_attr = {24576, 4, 0, 80, 0, 1};
    struct ak_audio_aec_attr default_ai_aec_attr = {0, 1024, 1024, 0, 512, 1, 0};
    struct ak_audio_aslc_attr default_ai_aslc_attr = {32768,volume,0};
    struct ak_audio_eq_attr default_ai_eq_attr = {0,10,{50, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{717, 717, 717, 717, 717, 717, 717, 717, 717, 717},
    {TYPE_HPF, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1, TYPE_PF1},
    0,0,0,0,0,0,0,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

    memset(&param, 0, sizeof(struct ak_audio_in_param));

    param.pcm_data_attr.sample_rate = AK_AUDIO_SAMPLE_RATE_8000;
    param.pcm_data_attr.sample_bits = AK_AUDIO_SMPLE_BIT_16;
    param.pcm_data_attr.channel_num = AUDIO_CHANNEL_MONO;
    param.dev_id = DEV_ADC;

    if (ak_ai_open(&param, &g_SdkMic_Handle))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_open failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_source(g_SdkMic_Handle, AI_SOURCE_MIC))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_source failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_nr_attr(g_SdkMic_Handle, &default_ai_nr_attr))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_nr_attr failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_agc_attr(g_SdkMic_Handle, &default_ai_agc_attr))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_agc_attr failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_aec_attr(g_SdkMic_Handle, &default_ai_aec_attr))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_aec_attr failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_aslc_attr(g_SdkMic_Handle, &default_ai_aslc_attr))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_aslc_attr failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_eq_attr(g_SdkMic_Handle, &default_ai_eq_attr))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_eq_attr failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_gain(g_SdkMic_Handle, gain))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_gain failed\n");
        return AK_FAILED;
    }

    if (ak_ai_set_volume(g_SdkMic_Handle, volume))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_volume failed\n");
        return AK_FAILED;
    }

    int frame_len = 0;
    if (ak_ai_get_frame_length(g_SdkMic_Handle, &frame_len))
    {
        ak_print_error_ex(MODULE_ID_APP, "ak_ai_set_frame_length failed\n");
        return AK_FAILED;
    }

    if (ak_ai_start_capture(g_SdkMic_Handle))
    {
        ak_print_error(MODULE_ID_APP, "*** ak_ai_start_capture failed. ***\n");
        return AK_FAILED;
    }

    int frame_interval = ak_audio_len_to_interval(&param.pcm_data_attr, frame_len);
    g_SdkMic_SleepTime = (frame_interval == 0) ? 16: (frame_interval / 2);

    Ext_SdkMediaCom_SetMic_CB_Register(SdkMic_Set_VolumeOrGain);
    Ext_SdkMediaCom_ResetMic_CB_Register(SdkMic_Reset_VolumeAndGain);

    return AK_SUCCESS;
}

int Ext_SdkMic_Exit(void)
{
    if (-1 != g_SdkMic_Handle)
    {
        /* ai close */
        ak_ai_close(g_SdkMic_Handle);
        g_SdkMic_Handle = -1;
    }

    return 0;
}

int Ext_SdkMediaCom_GetMicData(pStSdkAudioInfo info)
{
    int ret = 0;

    memset(&g_SdkMic_Frame,0,sizeof(g_SdkMic_Frame));

    ret = ak_ai_get_frame(g_SdkMic_Handle, &g_SdkMic_Frame, 0);
    if (ret)
    {
        ak_sleep_ms(g_SdkMic_SleepTime);
        return DEF_COM_FAILED;
    }

    if (!g_SdkMic_Frame.data || g_SdkMic_Frame.len <= 0)
    {
        ak_sleep_ms(10);
        return DEF_COM_FAILED;
    }

    if (Ext_InSdkBasicCfg_GetVal(BASIC_CFG_MIC_DISABLE))
    {
        memset(g_SdkMic_Frame.data,0,g_SdkMic_Frame.len);
    }

    info->size = g_SdkMic_Frame.len;
    info->buf = g_SdkMic_Frame.data;
    info->pts = g_SdkMic_Frame.ts;

    return DEF_COM_SUCCESS;
}

void Ext_SdkMediaCom_ReleaseMicData(void)
{
    ak_ai_release_frame(g_SdkMic_Handle, &g_SdkMic_Frame);
}
